<?
require_once('magpie/rss_fetch.inc');
$url = "http://sandiegoshortsaleblog.com/feed";
//$url = $_GET['url'];
$rss = fetch_rss( $url );

//Get all the channel info
if($rss->channel['source_encoding'] != '')
{
	$encoding = $rss->channel['source_encoding'];
}
else
{
	$encoding = 'UTF-8';
}

echo "<pre>ss";var_dump($rss->items);exit;
//$maintitle = $rss->channel['title'];
//$mainlink = $rss->channel['link'];
//$maindesc = $rss->channel['description'];
$maintitle = mb_convert_encoding($rss->channel['title'],'HTML-ENTITIES', $encoding);
$mainlink = mb_convert_encoding($rss->channel['link'],'HTML-ENTITIES', $encoding);
$maindesc = mb_convert_encoding($rss->channel['description'],'HTML-ENTITIES', $encoding);
$feed_type = $rss->feed_type;
$feed_version = $rss->feed_version;

//echo $rss."<BR><BR>";
//echo $mainlink."<BR><BR>";

$insert = "Insert into Blog_Feeds (Url, Date_Created, Owner, Website, Title, Description, Category_ID, encoding, feed_type, feed_version, City, State, Latitude, Longitude) Values ('".$url."', '".date('Y-m-d')."', '".$_SESSION['id']."', '".$mainlink."', '".mysql_real_escape_string($maintitle)."', '".mysql_real_escape_string($maindesc)."', '".$category."', '".$encoding."', '".$feed_type."', '".$feed_version."', '".$city."', '".$state."', '".$lat."', '".$lng."')";
echo $insert;
$iresult = mysql_query($insert) or die ("SQL Error".mysql_error());
$insert_id_feed = mysql_insert_id();

$next_read = mktime(date('H')+2, date('i'), date('s'), date('m'), date('d'), date('Y'));
$next_read = date('Y-m-d H:i:s', $next_read);

$update = "update Blog_Feeds set Last_Read = '".date('Y-m-d H:i:s')."', Next_Read = '".$next_read."' where ID = '".$insert_id_feed."'";
$iresult = mysql_query($update) or die ("SQL Error".mysql_error());

if($tags !='')
{
	$tags2 = explode(",", $tags);

	$i = 0;

	while($i < count($tags2))
	{
		$insert = "Insert into Blog_Tags (Blog_Feed_ID, Tag, Owner) Values ('".$insert_id_feed."', '".$tags2[$i]."', '".$_SESSION['id']."')";
		$iresult = mysql_query($insert) or die ("SQL Error".mysql_error());
		$i++;
	}
}
$i=0;

//submit all the articles and categories

if($insert_id_feed != "")
{
	$count = 0;
	if(strtoupper($feed_type) == "ATOM")
	{
		$count = 1;
		foreach ($rss->items as $item)
		{
			if($item['atom_content'] == "")
			{
				$array = $item['content'];
				$content = $array['encoded'];
			}
			else
			{
				$content = $item['atom_content'];
			}


			if($item['author_name'] != "")
			{
				$author = $item['author_name'];
			}
			else
			{
				if($item['author'] != "")
				{
					$author = $item['author'];
				}
				else
				{
					$aarray = $item['dc'];
					$author = $aarray['creator'];
				}
			}

			$categories = $item['category'];
			$array = $item['dc'];
			$categories .= $array['subject'];


			$created = date('Y-m-d H:i:s', $item['date_timestamp']);

			$c = insert_articles($item['title'], $item['link'], $content, $created, mysql_real_escape_string($author), $categories, $encoding, $insert_id_feed);
			//echo "ATOM:<BR>TITLE: ".$item['title']."<BR>LINK: ". $item['link']."<BR>CONTENT: ".$content."<BR>DATE: ".$created."<BR>AUTHOR: ".$author."<BR>CATEGORIES: ".$item['category']."<BR><BR><BR>";
			$count++;
		}
	}

	if(strtoupper($feed_type) == "RSS" && $feed_version == "1.0")
	{
		$count = 0;
		foreach ($rss->items as $item)
		{

			$content = $item['description'];

			if($item['author'] != "")
			{
				$author = $item['author'];
			}
			else
			{
				$aarray = $item['dc'];
				$author = $aarray['creator'];
			}


			$categories = $item['category'];
			$array = $item['dc'];
			$categories .= $array['subject'];


			$created = date('Y-m-d H:i:s', $item['date_timestamp']);

			$c = insert_articles($item['title'], $item['link'], $content, $created, mysql_real_escape_string($author), $categories, $encoding, $insert_id_feed);
			//echo "RSS1<BR>TITLE: ".$item['title']."<BR>LINK: ". $item['link']."<BR>CONTENT: ".$content."<BR>DATE: ".$created."<BR>AUTHOR: ".$author."<BR>CATEGORIES: ".$item['category']."<BR><BR><BR>";
			$count++;
		}
	}

	if(strtoupper($feed_type) == "RSS" && $feed_version == "2.0")
	{
		$count = 0;
		foreach ($rss->items as $item)
		{

			if($item['atom_content'] == "")
			{
				$array = $item['content'];
				$content = $array['encoded'];
			}
			else
			{
				$content = $item['atom_content'];
			}
			if($item['description'] != "")
			{

				$content = $item['description'];
			}

			if($item['author_name'] != "")
			{
				$author = $item['author_name'];
			}
			else
			{
				if($item['author'] != "")
				{
					$author = $item['author'];
				}
				else
				{
					$aarray = $item['dc'];
					$author = $aarray['creator'];
				}

			}

			$categories = $item['category'];
			$array = $item['dc'];
			$categories .= $array['subject'];

			$created = date('Y-m-d H:i:s', $item['date_timestamp']);

			$c = insert_articles($item['title'], $item['link'], $content, $created, mysql_real_escape_string($author), $categories, $encoding, $insert_id_feed);
			//echo "RSS2<BR>TITLE: ".$item['title']."<BR>LINK: ". $item['link']."<BR>CONTENT: ".$content."<BR>DATE: ".$created."<BR>AUTHOR: ".$author."<BR>CATEGORIES: ".$item['category']."<BR><BR><BR>";
			$count++;
		}

	}
}

//$result = "Added ".$count." articles to the database.";

$result = 1;
return $result;
